#include <iostream>
using namespace std;

const int kMaxN = 5010;
const int kInf = 1e9;

int f[kMaxN], cnt[kMaxN];

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int T;
  for (cin >> T; T; T--) {
    int n;

    cin >> n;
    fill(cnt, cnt + n + 1, 0);
    fill(f, f + n + 1, kInf);
    for (int i = 0; i < n; i++) {
      int a;
      cin >> a;
      if (a < n) {
        cnt[a]++;
      }
    }
    int st = 0;
    for (; cnt[st]; st++);
    f[st] = 0;
    for (int i = st - 1; i >= 0; i--) {
      for (int j = i + 1; j <= st; j++) {
        f[i] = min(f[i], f[j] + (cnt[i] - 1) * j + i);
      }
    }
    cout << f[0] << "\n";
  }
  return 0;
}